﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BubbleSwitch : MonoBehaviour
{
    /// <summary>
    /// This script is to control the effect when two bubbles merge and delight.
    /// </summary>
    [Header("DarkgrayBubble")]
    public GameObject DarkGray;
    [Header("GoldenBubble")]
    public GameObject Golden;
    [Header("InterpolationPara")]
    public float LerpSpeed;
    [Tooltip("The times of lerpSpeed")]
    public float DimingBackSpeed;//回退到灰色
    [Header("CurrentStatus")]
    public BubbleStatus _current;
    Material Dg;
    Material Gdn;
    BubbleTouch BlendingType;
    public enum BubbleTouch
    {
        G2G = 0,//golden to golden
        D2G = 1,//golden to darkgray
        D2D = 2,
        Default = -1,
    }
    public enum BubbleStatus
    {
        Darkgray = 0,
        Golden = 1,
        OnDimingBack = 2,
        OnBecomingGolden = 3,
    }
    int message;
    float transparentDg;
    float transparentGdn;
    private void Awake()
    {
        //_current = BubbleStatus.Darkgray;
        Dg = DarkGray.GetComponent<MeshRenderer>().material;
        Gdn = Golden.GetComponent<MeshRenderer>().material;
        //Gdn.color = new Color(Gdn.color.r, Gdn.color.g, Gdn.color.b, 0);
        transparentDg = Dg.color.a;
        transparentGdn = Gdn.color.a;
        message = -1;//初始化信号
        if (_current == BubbleStatus.Darkgray)
        {
            Gdn.color = new Color(Gdn.color.r, Gdn.color.g, Gdn.color.b, 0);
        }
        else
        {
            Dg.color = new Color(Dg.color.r, Dg.color.g, Dg.color.b, 0);
        }
    }
    private void Start()
    {
        StartCoroutine(DimBack2Darkgray(Dg, Gdn));
    }
    // Update is called once per frame
    void Update()
    {
        if (BlendingType == BubbleTouch.D2G)
        {
            StartCoroutine(Darkgray2Golden(Dg, Gdn));
            BlendingType = BubbleTouch.Default;
        }

    }

    private void OnTriggerExit(Collider other)
    {
        if (other.GetComponent<BubbleSwitch>() != null)
            if (other.GetComponent<BubbleSwitch>()._current == BubbleSwitch.BubbleStatus.Darkgray)
            {

            }
            else if (other.GetComponent<BubbleSwitch>()._current == BubbleSwitch.BubbleStatus.Golden)
            {
                message = 0;
            }

        if (other.CompareTag("WoodBench"))
        {
            message = 5;
            StartCoroutine(DimBack2Darkgray(Dg, Gdn));
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.gameObject.name);
        if (other.GetComponent<BubbleSwitch>() != null)
            if (other.GetComponent<BubbleSwitch>()._current == BubbleSwitch.BubbleStatus.Darkgray)//或者变金
            {
                if (_current == BubbleStatus.Darkgray)
                {
                    BlendingType = BubbleTouch.D2D;
                    message = 1;
                }
            }
            else if (other.GetComponent<BubbleSwitch>()._current == BubbleSwitch.BubbleStatus.Golden)//正在变灰
            {
                if (_current == BubbleStatus.Golden)
                {
                    BlendingType = BubbleTouch.G2G;
                    message = 2;
                }
                else if (_current == BubbleStatus.Darkgray)
                {
                    BlendingType = BubbleTouch.D2G;
                    message = 3;
                }
            }
        if (other.CompareTag("WoodBench"))
        {
            message = 6;
            StartCoroutine(Darkgray2Golden(Dg, Gdn));
        }
    }
    IEnumerator Darkgray2Golden(Material Dg, Material Gdn)
    {
        while (true)
        {
            yield return new WaitForEndOfFrame();
            Dg.color = Color.Lerp(Dg.color, new Color(Dg.color.r, Dg.color.g, Dg.color.b, 0), LerpSpeed * Time.deltaTime);
            Gdn.color = Color.Lerp(Gdn.color, new Color(Gdn.color.r, Gdn.color.g, Gdn.color.b, transparentGdn), LerpSpeed * Time.deltaTime);
            if (Mathf.Abs(Gdn.color.a - transparentGdn) < 10e-2)
            {
                Gdn.color = new Color(Gdn.color.r, Gdn.color.g, Gdn.color.b, transparentGdn);
                Dg.color = new Color(Dg.color.r, Dg.color.g, Dg.color.b, 0);
                _current = BubbleStatus.Golden;
                break;
            }
            if (message == 0 || message == 5)//stop merging
            {
                message = -1;
                break;
            }
        }
    }
    IEnumerator DimBack2Darkgray(Material Dg, Material Gdn)
    {
        while (true)
        {
            yield return new WaitForEndOfFrame();
            Dg.color = Color.Lerp(Dg.color, new Color(Dg.color.r, Dg.color.g, Dg.color.b, transparentDg), DimingBackSpeed * Time.deltaTime);
            Gdn.color = Color.Lerp(Gdn.color, new Color(Gdn.color.r, Gdn.color.g, Gdn.color.b, 0), DimingBackSpeed * Time.deltaTime);
            if (Mathf.Abs(Dg.color.a - transparentDg) < 10e-2)
            {
                Gdn.color = new Color(Gdn.color.r, Gdn.color.g, Gdn.color.b, 0);
                Dg.color = new Color(Dg.color.r, Dg.color.g, Dg.color.b, transparentDg);
                _current = BubbleStatus.Darkgray;
                break;
            }
        }
    }
}
